package com.bjsxt.service.impl;

import com.bjsxt.mapper.MenuMapper;
import com.bjsxt.pojo.Menu;
import com.bjsxt.pojo.PageA;
import com.bjsxt.pojo.TreeA;
import com.bjsxt.service.MenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    //查询指定用户的菜单
    @Override
    public List<TreeA> findMore(Integer aid, Integer pid) {
        List<Menu> list = menuMapper.selectMore(aid, pid);
        List<TreeA> li=new ArrayList<>();
        for (Menu menu:list){
            TreeA t=new TreeA(menu.getMid(),menu.getMname(),menu.getStatus()==0?"closed":"open",menu.getUrl());
           li.add(t);
        }
        return li;
    }
    //查询菜单响应到数据网格中
    @Override
    public PageA<Menu> findMore2(Integer page, Integer rows) {
        Page pg = PageHelper.startPage(page, rows);
        List<Menu> list = menuMapper.selectAll();
        PageA<Menu> pga=new PageA<Menu>(list,pg.getTotal());

        return pga;
    }
    //查询菜单与用户的权限无关
    @Override
    public List<TreeA> findMore3(Integer pid) {
        List<Menu> list = menuMapper.selectMore2(pid);
        List<TreeA> li=new ArrayList<>();
        for (Menu menu:list){
            TreeA t=new TreeA(menu.getMid(),menu.getMname(),menu.getStatus()==0?"closed":"open",menu.getUrl());
            li.add(t);
        }
        return li;
    }
   //添加菜单信息
    @Override
    public Integer saveMenu(Menu menu) {
        return menuMapper.insert(menu);
    }
    //修改菜单信息
    @Override
    public Integer updateMenu(Menu menu) {
        return menuMapper.update(menu);
    }

    @Override
    public Integer detele(Integer mid) {
        /*查询此编号作为其他菜单的父编号*/
        List<Menu> list = menuMapper.selectMore2(mid);
        //判断是否有子节点
        if (list.size()>0){
            return -1;
        }
        //没有进行删除
        return menuMapper.delete(mid);
    }
    //查询同步树的菜单
    @Override
    public List<TreeA> findMore4() {
        //获得一级菜单
        List<TreeA> list = menuMapper.selectMore3(0);
        //遍历一级菜单
        for (TreeA t:list){
            //获得一级菜单对象中的id
            Integer mid = t.getId();
            //获当前一级菜单对象下的二级菜单对象
            List<TreeA> list1 = menuMapper.selectMore3(mid);
            //把二级菜单放在一级菜单中
            t.setChildren(list1);
        }
        return list;
    }
}
